<?php

namespace addons\qingdong\controller;

use addons\qingdong\model\ExamineRecord;
use addons\qingdong\model\Consume as ConsumeModel;
use addons\qingdong\model\Message;
use addons\qingdong\model\File;
use addons\qingdong\model\Staff;
use think\Db;
use think\Exception;

/**
 * 费用接口
 */
class Consume extends StaffApi
{
    protected $noNeedLogin = [];
    protected $noNeedRight = [];


    //添加费用
    public function addConsume()
    {
        $params = $this->request->post();

        // 表单验证
        if (($result = $this->qingdongValidate($params, get_class(), 'create')) !== true) {
            $this->error($result);
        }
        Db::startTrans();
        try {
            $result = ConsumeModel::createConsume($params);
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if ($result) {
            $this->success('添加费用成功');
        }
    }

    //编辑费用
    public function editConsume()
    {
        $params = $this->request->post();

        // 表单验证
        if (($result = $this->qingdongValidate($params, get_class(), 'create')) !== true) {
            $this->error($result);
        }
        Db::startTrans();
        try {
            $result = ConsumeModel::updateConsume($params);
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if ($result) {
            $this->success('添加费用成功');
        }
    }

    //获取费用列表
    public function getList()
    {
        $limit = input("limit/d", 10);
        $customer_id = input('customer_id', '', 'intval');
        $times = input('times', '');
        $status = input('check_status',0);
        $where = [];

        $params = $this->request->post();
        if ($times) {//
            $times = explode(',', $times);
            $where['createtime'] = ['between', [strtotime($times[0]), strtotime($times[1]) + 86400 - 1]];
        }
        if (isset($params['staff_id']) && $params['staff_id']) {//下级员工筛选
            $where['staff_id'] = $params['staff_id'];
        } else {
            $where['staff_id'] = ['in', Staff::getMyStaffIds()];
            if (isset($params['type']) && $params['type']) {//客户分类
                if ($params['type'] == 1) {//我的客户
                    $where['staff_id'] = $this->auth->id;
                } elseif ($params['type'] == 2) {//下属负责的客户
                    $where['staff_id'] = ['in', Staff::getLowerStaffId()];
                }
            }
        }

        if ($status) {
            if($status == 1){//待审核
                $where['check_status']=['in',[0,1]];
            }elseif($status == 2){//通过
                $where['check_status']=2;
            }elseif($status == 3){//审核拒绝
                $where['contract_status']=['in',[3,4]];
            }
        }
        if($customer_id){
            $where['customer_id'] = $customer_id;
        }
        $records = ConsumeModel::where($where)->with(['staff','customer'])->order('id desc')->paginate($limit)->toArray();
        $remoney = ConsumeModel::where(['staff_id'=>$where['staff_id']])
            ->where(array('check_status'=>2))->sum('money');
        $inmoney = ConsumeModel::where(['staff_id'=>$where['staff_id']])
            ->where(array('check_status'=>['in',[0,1]]))->sum('money');
        $nomoney = ConsumeModel::where(['staff_id'=>$where['staff_id']])
            ->where(array('check_status'=>['in',[3,4]]))->sum('money');
        $moneyinfo['remoney'] = $remoney;//已回款
        $moneyinfo['inmoney'] = $inmoney;//回款中
        $moneyinfo['nomoney'] = $nomoney;//未回款
        $moneyinfo['allmoney'] = $remoney+$inmoney+$nomoney;//总金额
        $records['moneyinfo']=$moneyinfo;
        $this->success('请求成功', $records);
    }


    //获取费用详情
    public function getDetail()
    {
        $id = input('id', '', 'intval');
        $consume = ConsumeModel::where(['id' => $id])->with(['staff','customer'])->find();
        if(empty($consume)){
            $this->error('数据不存在');
        }
        $consume['files'] = File::where(['id' => ['in', explode(',', $consume['file_ids'])]])->field('id,types,name,file_path')->select();

        if ($consume['check_status'] == 0 || $consume['check_status'] == 1) {
            $consume['is_examine'] = ExamineRecord::isExaminse(ExamineRecord::CONSUME_TYPE, $id);
        } else {
            $consume['is_examine'] = 0;
        }
        //标记通知已读
        Message::setRead(Message::CONSUME_TYPE, $id, $this->auth->id);

        $this->success('请求成功', $consume);
    }

}
